$(function () {

    $("#all_types").click(function () {
        const $all_type = $(this);
        const $span = $all_type.find("span").find("span");
        const $all_types_container = $("#all_types_container");

        //如果分类已经打开，那再点一次则关闭
        if ($all_types_container.css("display") !== 'none') {
            $all_types_container.slideUp();
            $span.removeClass("glyphicon-chevron-up").addClass("glyphicon-chevron-down");
            return 0;
        }
        $all_types_container.slideDown();
        $span.removeClass("glyphicon-chevron-down").addClass("glyphicon-chevron-up");
        // 全部分类打开时，把综合排序给关掉
        $("#sort_container").hide();
        $("#sort_rule").find("span").find("span").removeClass("glyphicon-chevron-up").addClass("glyphicon-chevron-down");

    });

    $("#all_types_container").click(function () {
        $(this).slideUp();
        const $all_type = $("#all_types");
        const $span = $all_type.find("span").find("span");
        $span.removeClass("glyphicon-chevron-up").addClass("glyphicon-chevron-down");

    });

    $("#sort_rule").click(function () {

        const $sort_rule = $(this);
        const $span = $sort_rule.find("span").find("span");
        const $sort_container = $("#sort_container");

        //如果分类已经打开，那再点一次则关闭
        if ($sort_container.css("display") !== 'none') {
            $sort_container.slideUp();
            $span.removeClass("glyphicon-chevron-up").addClass("glyphicon-chevron-down");
            return 0;
        }

        $sort_container.slideDown();
        $span.removeClass("glyphicon-chevron-down").addClass("glyphicon-chevron-up");
        $("#all_types_container").hide();
        $("#all_types").find("span").find("span").removeClass("glyphicon-chevron-up").addClass("glyphicon-chevron-down");
    });

    $("#sort_container").click(function () {
        $(this).slideUp();
        $("#sort_rule").find("span").find("span").addClass("glyphicon-chevron-down").removeClass("glyphicon-chevron-up");
    });

//    添加商品到购物车
    $(".addShopping").click(function () {
        //    拿到商品id发送给服务器
        var $addShop = $(this);
        // 获取属性，属性在html中写了
        var goodsid = $addShop.attr("goodsid");
        //console.log(goodsid);
        // console.log($(this).attr("class"));
        // console.log("**************")
        // var goodsid2 = $(this).prop("goodsid");
        // console.log(goodsid2);
        // console.log($(this).prop("class"));

        $.get("/axf/addtocart/", {"goodsid": goodsid}, function (data) {
            console.log(data);
            if (data["status"] === 302) {
                window.open("/axf/login/", target = "_self");
            } else if (data["status"] === 200) {
                var g_num = data["c_goods_num"];
                //查找点击节点的兄弟节点（上一个节点）
                var span_num = $addShop.prev();
                //修改它的值
                span_num.html(g_num);
            }
        })

    });

    // 从购物车减少商品
    $(".subShopping").click(function () {
        //    拿到商品id发送给服务器
        var subShop = $(this);
        var goodsid = $(this).attr("goodsid");
        // console.log(goodsid);
        // console.log($(this).attr("class"));
        // console.log("**************")
        // var goodsid2 = $(this).prop("goodsid");
        // console.log(goodsid2);
        // console.log($(this).prop("class"));

        $.getJSON("/axf/subtocart/", {"goodsid": goodsid}, function (data) {
            console.log(data);
            if (data["status"] === "901") {
                window.open("/axf/login/", target = "_self");
            } else if (data["status"] === "200") {
                var g_num = data["g_num"];
                var span_num = subShop.next();
                span_num.html(g_num);
            } else if (data["status"] === "902") {
                alert(data["msg"]);
            }
        })
    })


})